package com.moxiao.yfjh.module.blind.service.comment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.comment.vo.MemberCommentCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.comment.vo.MemberCommentExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.comment.vo.MemberCommentPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.comment.vo.MemberCommentUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.comment.MemberCommentConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.comment.MemberCommentDO;
import com.moxiao.yfjh.module.blind.dal.mysql.comment.MemberCommentMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.comment.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员评价 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberCommentServiceImpl implements MemberCommentService {

    @Resource
    private MemberCommentMapper memberCommentMapper;

    @Override
    public Long createMemberComment(MemberCommentCreateReqVO createReqVO) {
        // 插入
        MemberCommentDO memberComment = MemberCommentConvert.INSTANCE.convert(createReqVO);
        memberCommentMapper.insert(memberComment);
        // 返回
        return memberComment.getId();
    }

    @Override
    public void updateMemberComment(MemberCommentUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberCommentExists(updateReqVO.getId());
        // 更新
        MemberCommentDO updateObj = MemberCommentConvert.INSTANCE.convert(updateReqVO);
        memberCommentMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberComment(Long id) {
        // 校验存在
        validateMemberCommentExists(id);
        // 删除
        memberCommentMapper.deleteById(id);
    }

    private void validateMemberCommentExists(Long id) {
        if (memberCommentMapper.selectById(id) == null) {
            throw exception(MEMBER_COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public MemberCommentDO getMemberComment(Long id) {
        return memberCommentMapper.selectById(id);
    }

    @Override
    public List<MemberCommentDO> getMemberCommentList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberCommentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberCommentDO> getMemberCommentPage(MemberCommentPageReqVO pageReqVO) {
        return memberCommentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberCommentDO> getMemberCommentList(MemberCommentExportReqVO exportReqVO) {
        return memberCommentMapper.selectList(exportReqVO);
    }

    @Override
    public MemberCommentDO getMemberCommentTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<MemberCommentDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MemberCommentDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(MemberCommentDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return memberCommentMapper.selectOne(lqw);
    }

    @Override
    public Integer getBlindMemberCommentNo(String commentNo) {
        return memberCommentMapper.getMemberCommentNo(commentNo);
    }

    @Override
    public Long selectCommentNum(Long momentId) {
        LambdaQueryWrapper<MemberCommentDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(MemberCommentDO::getMomentId,momentId);
        return memberCommentMapper.selectCount(lqw);
    }

}
